package com.ericsson.resourceplanner.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ericsson.resourceplanner.base.AbstractController;
import com.ericsson.resourceplanner.dao.entity.Report;
import com.ericsson.resourceplanner.exceptions.ResourcePlannerException;
import com.ericsson.resourceplanner.model.ArrayListOfReport;
import com.ericsson.resourceplanner.model.Error;
import com.ericsson.resourceplanner.service.AdminServ;
import com.ericsson.resourceplanner.utils.ResourcePlannerConstants;

@Controller
public class ExportController extends AbstractController{
	public static final Logger logger=LoggerFactory.getLogger(ExportController.class);
	@Autowired
	private AdminServ adminService;
	@RequestMapping(value="/download/getReport" , method=RequestMethod.GET)
	public void exportDateRangedReport(HttpServletRequest request,HttpServletResponse response,Model model){
		String startDate=request.getParameter("minDate");
		String endDate=request.getParameter("maxDate");
		Workbook workbook=null;
		try {
			if(startDate==null || endDate ==null || startDate.length()==0 || endDate.length()==0){
				logger.debug("problem in input data ::  StartDate , EndDate"+":: "+ startDate+" , " + endDate+":: ");
				throw new ResourcePlannerException("Either Start Date or End Date is null");
			}else{
				logger.debug("Before Exporting Data from database :: StartDate , EndDate"+":: "+ startDate+" , " + endDate+":: ");
				workbook= adminService.getExportDataFromDataBase(startDate, endDate);
				logger.debug("After Exporting Data from database ::");
			}
			
			if(workbook!=null){
				String fileName=ResourcePlannerConstants.EXCEL_REPORT_FIRSTNAME;
				String  dateRange=startDate+ResourcePlannerConstants.HYPHEN+endDate;
				fileName=fileName+ResourcePlannerConstants.UNDERSCORE+dateRange+
						ResourcePlannerConstants.EXCEL_REPORT_EXTENSION_XLS;
				
				writeExcelInResposne(request, response, workbook, fileName);
			}else{
				throw new ResourcePlannerException("Received Null Workbook");
			}
		} catch (ResourcePlannerException e) {
			logger.error(e.getMessage(), e);
			Error error= new Error();
			error.setCause(e.getMessage());
			error.setMessage("Something Went Wrong");
			writeJsonDataInResposne(request, response, toJson(error));
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			Error error= new Error();
			error.setCause(e.getMessage());
			error.setMessage("Something Went Wrong");
			writeJsonDataInResposne(request, response, toJson(error));
		}finally{
			adminService.closeWorkbook(workbook);
		}
		
		
	}
	
	@RequestMapping(value="/download",method=RequestMethod.GET)
	public String download(){
		
		return "testExport";
	}
	
	@RequestMapping(value="/download/current",method=RequestMethod.POST)
	public void downloadMonthWiseReport(HttpServletRequest request, HttpServletResponse response,@RequestBody ArrayListOfReport listOfReport){
		//TODO check .. Json to Object
		//TODO check the month- year parameter names ---
		String month=request.getParameter("month");
		String year=request.getParameter("year");
		Workbook workbook=null;
		
		String name=month+year;
		String fileName="Report"+ResourcePlannerConstants.UNDERSCORE+month+ResourcePlannerConstants.HYPHEN+year;
		if(name!=null && name.length()>0){
			if(listOfReport!=null){
				workbook=adminService.callCreateExcel(name, listOfReport.getReports());
			}else{
				workbook=adminService.callExportMonthlyReport(month, year);
			}
			if(workbook!=null){
				writeExcelInResposne(request, response, workbook, fileName);
			}
		}
	}
	
		@RequestMapping(value="/download/prepare", method=RequestMethod.GET)
		public void prepareJson(HttpServletRequest request,HttpServletResponse response){
			//TODO -- delete -

			Report report= new Report();
			report.setCustomer("Customer Name");
			report.setDomain("Domain Name");
			report.setEmpName("Emp  Name");
			report.setICRRB("IRCBB");
			report.setProject("project");
			report.setRegion("region");
			report.setSignum("SIGNUM");
			report.setStatus("status");
			report.setUtilPercent("100%");
			report.setW1("1");
			report.setW2("w2");
			report.setW3("w2");
			report.setW4("w2");
			
			
			Report report2=report;
			List<Report> reports= new ArrayList<Report>();
			reports.add(report);
			reports.add(report2);
			ArrayListOfReport listOfReport= new ArrayListOfReport();
			listOfReport.setReports(reports);
			
			writeJsonDataInResposne(request, response, toJson(listOfReport));
			
		
		}
	
	
}
